package com.lovo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.bean.Supplier;
import com.lovo.bean.Tender;
import com.lovo.bean.Tenderinfo;
import com.lovo.dto.TenderDto;
import com.lovo.dto.TenderInfoDto;
import com.lovo.mapper.TenderinfoMapper;
import com.lovo.service.ISupplierService;
import com.lovo.service.ITenderService;
import com.lovo.service.ITenderinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lovo
 * @since 2022-09-17
 */
@Service
public class TenderinfoServiceImpl extends ServiceImpl<TenderinfoMapper, Tenderinfo> implements ITenderinfoService {

    @Autowired
    ITenderService tenderService;

    @Autowired
    ISupplierService supplierService;

    @Autowired
    TenderinfoMapper tenderinfoMapper;
    @Autowired
    MQSService service;

    @Override
    public Tenderinfo sendTendInfo(Integer supplierId, Integer price, Integer tenderId, String info, Integer productId) {
        Tenderinfo tenderinfo = new Tenderinfo();
        Tender tender = tenderService.getBaseMapper().selectById(tenderId);
        Supplier supplier = supplierService.getBaseMapper().selectById(supplierId);
        tenderinfo.setFkSupplierid(supplierId);
        tenderinfo.setSuppliername(supplier.getUSuppliername());
        tenderinfo.setIPrice(price);
        tenderinfo.setIInfo(info);
        tenderinfo.setTenderid(tenderId);
        tenderinfo.setIProductname(tender.getProductname());
        tenderinfo.setINum(tender.getTCount());
        tenderinfo.setIType(tender.getTType());
        tenderinfo.setIStatus("投递中");
        tenderinfo.setITime(String.valueOf(LocalDate.now()));
        tenderinfo.setIProductId(productId);


        tenderinfoMapper.insert(tenderinfo);
        TenderInfoDto tenderInfoDto = new TenderInfoDto();
        BeanUtils.copyProperties(tenderinfo, tenderInfoDto);
        tenderInfoDto.setFkTender(tenderinfo.getTenderid());

        service.sendTenderInfo(tenderInfoDto);
        return tenderinfo;
    }

    @Override
    public IPage<Tenderinfo> findAllTenderInfoBySupId(Integer PageNO, Integer supId) {
        QueryWrapper<Tenderinfo> qw = new QueryWrapper<>();
        qw.eq("fk_supplierId", supId);
        return this.getBaseMapper().selectPage(new Page(PageNO, 5), qw);
    }

    @Override
    public Tenderinfo findTenderInfoById(Integer tenderInfoId) {
        return this.getBaseMapper().selectById(tenderInfoId);
    }


    @Override
    public boolean updateTenderInfoStatus(Integer tenderInfoId) {
        Tenderinfo tenderinfo = baseMapper.selectById(tenderInfoId);
        Tender tender = tenderService.getBaseMapper().selectById(tenderinfo.getTenderid());
        tender.setTStatus("已招标");
        tenderService.getBaseMapper().updateById(tender);

        QueryWrapper<Tenderinfo> qw = new QueryWrapper<>();
        qw.eq("tenderId", tenderinfo.getTenderid());
        List<Tenderinfo> list = baseMapper.selectList(qw);

        list.forEach(n -> {
            n.setIStatus("未中标");
            baseMapper.updateById(n);
        });

        tenderinfo.setIStatus("已中标");
        baseMapper.updateById(tenderinfo);
        return true;
    }





}
